#ifndef COMP_COM_H_
#define COMP_COM_H_

#include <config.h>

#include "commands.h"

int command(String x, char** b);

String inputString = "";
int count = 0;

bool valid = false;

int MAX = (2 * NUM_COMMAND_COMS) + 1;

int comp_com_init(){
  // For computer
  Serial.begin(ROVER_ARDUI_BAUD);
  inputString.reserve(200);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void sendToComp(char* input){
  Serial.println(input);
}

void send_to_comp(String inputString){
  String output = "";
  int length = inputString.length();
  
  char buff[inputString.length() + 1];
  inputString.toCharArray(buff, inputString.length() + 1);
  
  // Assume correct delinate
  for(int i = 0; i < inputString.length(); ++i){
    if(i % 2 == 0){
      output += inputString.charAt(i);
    }else{
      unsigned char c = inputString.charAt(i);
      int intForm = (int)c;
      output += intForm;
    }
  }

  Serial.println(output);
}

void serialEvent(){
  while(Serial.available()){
    char inChar = (char)Serial.read();
    inputString += inChar;
    count++;
    if(inChar == STRING_STAR_POINT){
      count = 1;
      inputString = "";
      inputString += STRING_STAR_POINT;
    }
    if(count == MAX){
      //      Serial.println(inputString);
      char* output;

      if(command(inputString, &output) < 0){

      }else{

      }

      send_to_comp(inputString);

      // Reset for next run
      inputString = "";
      count = 0;
    }
  }
}

#endif
